#This script executes event-study regressions to estimate cohort-specific 
#associations between first-generation prenatal exposure to nonattainment 
#and first-generation economic outcomes.

rm(list=ls())
gc()
library(reldist)
library(ineq)
library(stargazer)
library(data.table)
library(dplyr)
library(dtplyr)
library(ggplot2)
library(stringdist)
library(lfe)
library(haven)
library(readr)
library(broom)
library(tidylog)
library(rdrobust)

# Set the working directory
setwd("/projects/opp_env/caa1970/")

# Source external R scripts for custom functions
source("Code/Child_Outcomes/child_outcomes_functions.R")
source("Code/rounding.r")

# Load the first-generation analysis dataset
load("Data/first_gen_analysis_data_jepmicro_acs.rda")

# Event Studies

## Fig B2a -- First-Gen Years of Schooling

eventB2a <- felm(years_of_schooling ~inst_1969+inst_1970+inst_1972+
                 inst_1973+inst_1974+inst_1975 |state_year+county_factor+year_factor+month+
                 survey_year_by_year | 0 |county_factor,
               data=parent_piks_acs%>% filter(year%in%1969:1975, AGE >33 & AGE < 44, real_wages<wage_cap,!is.na(parent_age_min)))
summary(eventB2a)

#Save the underlying point
figB2a <- eventB2a %>% tidy() %>% mutate_each(funs(signif(.,4)),-term)  %>% 
  filter(grepl("inst", term), !is.na(estimate))%>%
  mutate(year=as.numeric(gsub("inst_","", gsub("_bin", "", term)))) %>% 
  bind_rows(., data.frame(estimate=0, std.error=0, year=1971)) %>% filter(year<=1975) 
write.csv(figB2a, file="JPE_Micro/Output/B2_Figures/FigB2a.csv", row.names=F)

## Fig B2b -- First-Gen High School Graduation

eventB2b <- felm(HS_grad ~inst_1969+inst_1970+inst_1972+
                 inst_1973+inst_1974+inst_1975 |state_year+county_factor+year_factor+month+
                 survey_year_by_year | 0 |county_factor,
               data=parent_piks_acs%>% filter(year%in%1969:1975, AGE >33 & AGE < 44, real_wages<wage_cap,!is.na(parent_age_min)))
summary(eventB2b)

#Save the underlying point
figB2b <- eventB2b %>% tidy() %>% mutate_each(funs(signif(.,4)),-term)  %>% 
  filter(grepl("inst", term), !is.na(estimate))%>%
  mutate(year=as.numeric(gsub("inst_","", gsub("_bin", "", term)))) %>% 
  bind_rows(., data.frame(estimate=0, std.error=0, year=1971)) %>% filter(year<=1975) 
write.csv(figB2b, file="JPE_Micro/Output/B2_Figures/FigB2b.csv", row.names=F)




eventB2c <- felm(some_college ~inst_1969+inst_1970+inst_1972+
                 inst_1973+inst_1974+inst_1975 |state_year+county_factor+year_factor+month+
                 survey_year_by_year | 0 |county_factor,
               data=parent_piks_acs%>% filter(year%in%1969:1975, AGE >33 & AGE < 44, real_wages<wage_cap,!is.na(parent_age_min)))
summary(eventB2c)

#Save the underlying point
figB2c <- eventB2c %>% tidy() %>% mutate_each(funs(signif(.,4)),-term)  %>% 
  filter(grepl("inst", term), !is.na(estimate))%>%
  mutate(year=as.numeric(gsub("inst_","", gsub("_bin", "", term)))) %>% 
  bind_rows(., data.frame(estimate=0, std.error=0, year=1971)) %>% filter(year<=1975) 
write.csv(figB2c, file="JPE_Micro/Output/B2_Figures/FigB2c.csv", row.names=F)

